<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <!-- header -->
    <%@ include file="/WEB-INF/views/include/header.jsp" %>
</head>
<body class="no-skin">
<%--上传成功模态框--%>
<div class="shade" style="display: none;"></div>
<div class="warning" style="display: none;">
    <p class="title"><em></em><span class="close">X</span></p>
    <p class="cont"></p>
    <div class="la-square-jelly-box la-3x">
        <div></div>
        <div></div>
    </div>
</div>
<!-- /section:basics/navbar.layout -->
<div class="main-container" id="main-container">

    <!-- /section:basics/sidebar -->
    <div class="main-content">
        <div class="main-content-inner">
            <div class="page-content">
                <!-- /section:settings.box -->
                <div class="row">
                    <div class="col-xs-12">
                        <div class="pull-left">
                            <button class="btn btn-sm btn-info" id="query">条件搜素</button>
                            <button class="btn btn-sm btn-success" id="create">新增任务</button>
                            <button class="btn btn-sm btn-info" id="detail">查看详情编辑</button>
                            <button class="btn btn-sm btn-default" id="flow">任务流程</button>
                            <button class="btn btn-sm btn-warning" id="comment">办结评价</button>
                            <button class="btn btn-sm btn-danger" id="delete">删除任务</button>
                            <button class="btn btn-sm btn-success" id="exportTaskNew">导出Excel</button>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12">
                        <!-- PAGE CONTENT BEGINS -->
                        <!-- jqGrid表格 -->
                        <table id="gridTable"></table>
                        <!-- jqGrid工具类 -->
                        <div id="gridPager"></div>
                        <!-- 查询对话框 -->
                        <div style="display: none;">
                            <div class="row" id="queryDialog"></div>
                        </div>
                        <!-- PAGE CONTENT ENDS -->
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->

    <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
        <i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
    </a>

    <%@ include file="/WEB-INF/views/include/dialog.jsp" %>
</div><!-- /.main-container -->
</div>

<!-- footer -->
<%@ include file="/WEB-INF/views/include/footer.jsp" %>

<script type="text/javascript">
    // 基本路径
    var baseUrl = "<s:url value="/project/"></s:url>"

    // jqgrid的表格高度
    var height = 320;
    // jqgrid表格行头的中文
    var colNames = ['','单号', '任务名称', "任务类型", "办理时限", "督查人", "任务状态","是否逾期","任务附件"];

    var colModel = [
        {name: 'sysId', width: 60, hidden: true, key: true},
        {name: 'sysId', index: 'sysId', width: 250, editable: true},
        {name: 'taskName', index: 'taskName', width: 250, editable: true},
        {name: 'categoryId', index: 'categoryId', width: 250, editable: true, formatter: categoryIdFormatter},
        {name: 'dateTime', index: 'dateTime', width: 250, editable: true},
        {name: 'operator', index: 'operator', width: 250, editable: true},
        {name: 'type', index: 'type', width: 120, editable: true, formatter: typeFormatter},
        {name: 'status', index: 'status', width: 120, editable: true,formatter: statusFormatter},
        {name: 'file', index: 'file', width: 150, editable: true, formatter: fileFormatter}
    ];

    /**
     * 逾期类型格式化
     */
    function statusFormatter(data) {
        return data == 0 ? '是 ' : ''
    }
    /**
     * 任务类型格式化
     */
    function categoryIdFormatter(data) {
        switch(data) {
            case '01':
                return '会议纪要';
                break;
            case '02':
                return '省市县为民办实事';
                break;
            case '03':
                return '省市县政府工作报告';
                break;
            case '04':
                return '政协提案办理';
                break;
            case '05':
                return '县长批示件';
                break;
            case '06':
                return '人大建议办理';
                break;
            case '07':
                return '其他事项';
                break;
            default:
                return '未知';
        }
    }

    /**
     * 任务状态格式化
     */
    function typeFormatter(data) {
        switch(data) {
            case '0':
                return '待签收';
                break;
            case '1':
                return '待反馈';
                break;
            case '2':
                return '已反馈';
                break;
            case '3':
                return '办结';
                break;
            case '4':
                return '未办结';
                break;
            default:
                return '未知';
        }
    }

    /**
     * 附件格式化
     */
    function fileFormatter(data) {
        if (data == null || data == "") {
            return "无任务附件";
        } else {
            return '<input type="button" value="下载" onclick="downDetail(\'' + data + '\')" \n' +
                ' style="width: 100%; height: 32px;" class="btn-large btn-primary"/>';
        }
    }

    //下载按钮
    function downDetail(id) {
        var url = "/filesUpload/download/" + id;
        window.open(url);
    }

    // jqgrid的具体执行方法
    jqgrid({
        gridTableId: "#gridTable",
        gridPagerId: "#gridPager",
        autoScroll: false,
        shrinkToFit: false,
        onSelectRow: function (rowid, status) {
            //选择当前的行
            var selectedRowValue = $("#gridTable").jqGrid('getRowData', rowid);

            var types = selectedRowValue.type;//获取任务状态
            //只有已经反馈和待反馈才可以打开
            if (types == "已反馈") {
                $("#comment").attr("disabled", false);
            } else {
                $("#comment").attr("disabled", true);
            }
            if (!status) {
                gridRemoveSelect(rowid);
            } else {
                var flag = false;
                for (var i = 0; i< rowids.length; i++){
                    if(rowids[i] ==  rowid){
                        flag = true;
                        continue;
                    }
                }
                if(!flag){
                    gridAddSelect(rowid);
                }
            }
        },
        loadComplete:function (data) {
            var ids = $("#gridTable").jqGrid("getDataIDs");
            console.log(ids);
            var rowDatas = $("#gridTable").jqGrid("getRowData");//获取所有行的数据
            for (var i = 0; i<rowDatas.length;i++){
                if (data.rows[i].status == 0){
                    $("#"+ids[i]+ " td").css("color","red");
                }
            }
        },
        onSelectAll:function (aRowids,status){
            if (!status) {
                aRowids.forEach(function(value,index){
                    gridRemoveSelect(value);
                });
            } else {
                aRowids.forEach(function(value,index){
                    var flag = false;
                    for (var i = 0; i< rowids.length; i++){
                        if(rowids[i] ==  value){
                            flag = true;
                            continue;
                        }
                    }
                    if(!flag){
                        gridAddSelect(value);
                    }
                });
            }
        },
        gridComplete: function () {
            var rowIds = jQuery("#gridTable").jqGrid('getDataIDs');
            console.log(rowIds);
            for (var k = 0; k < rowIds.length; k++) {
                var flag = false;
                for (var i = 0; i < rowids.length; i++) {
                    if (rowids[i] == rowIds[k]) {
                        flag = true;
                        continue;
                    }
                }
                if (flag && flag == true) {
                    jQuery("#gridTable").setSelection(rowIds[k], true);
                }
            }
        }
    });

    //清空选中行
    $("#cancelChecked").click(function () {
        $("#gridTable").jqGrid('resetSelection');
        rowids=[];
    });
    var rowids = [];//选中行id
    /**
     * 取消选中
     */
    function gridRemoveSelect(rowid) {
        rowids.remove(rowid);
    }

    /**
     * 添加选中
     */
    function gridAddSelect(rowid) {
        rowids.push(rowid);
    }

    /**
     * 重写Array
     */
    Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };

    /**
     * 重写Array
     */
    Array.prototype.indexOf = function (val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == val) return i;
        }
        return -1;
    };
    //导出表

$("#exportTaskNew").click( function () {
    showExcelDialog({
        url: "/project/exportTaskNew?sysIds=",
        tableId: "#gridTable",
        confirmTitle: "确认对话框",
        confirmMessage: "是否确定导出?",
        confirmOk: "确定",
        confirmCancel: "取消",
        successMessage: "删除成功",
        successTitle: "消息对话框",
        failMessage: "导出失败",
        failTitle: "消息对话框",
        errorTitle: "警告提示框",
        errorMessage: "您当前尚未选中一行，请选择后再试",
        errorOk: "确定"
    });

})


    //重置表单
    function reset() {
        resetForm("#formDialog form");
    }

    //加载查询对话框
    $("#queryDialog").load(getUrl("filter"));

    //显示查询对话框
    var queryDialog = $("#queryDialog");
    $("#query").click(function () {
        showDialog({
            dialogTitle: '条件搜素',
            dialogBody: queryDialog,
            dialogFoot: "<button type='button' class='btn btn-primary' onclick='query()'>查询</button>" +
            "<button type='button' class='btn btn-pink' onclick='reset()'>重置</button>" +
            "<button type='button' class='btn btn-default' data-dismiss='modal'>关闭</button>"
        });
    });

    //具体查询方法
    function query() {
        var exportData = $('#formDialog form').serialize();
        seachJqgird("#gridTable", "#formDialog form");
        $("#formDialog").modal('hide');
    }

    // 重置创建表单
    function resetCreate() {
        resetForm("#formDialog form");
        // 清除关于会议纪要的内容,及隐藏按钮
        $('#departContent').empty();
        $("#addDepart").css("display", "none");
    }

    // 显示创建对话框
    $("#create").click(function () {
        loadDialog({
            dialogTitle: '新增任务',
            dialogUrl: getUrl("create"),
            dialogFoot: '<button type="button" class="btn btn-primary" onclick="create()">添加</button>' +
            '<button type="button" class="btn btn-pink" onclick="resetCreate()">重置</button>' +
            '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>'
        });
    });

    // 保存数据
    function create() {

        // 先验证表单
        if(!$("#formDialog form").valid()) {
            return;
        }

        var hasDepart = $('#departContent').html();
        if (hasDepart == null || hasDepart == '') {
            alertMessage("请添加承办单位!");
            return;
        }

        // 判断承办单位是否重复
        var departIdList = new Array();
        $("select[name$='.userId']").each(function () {
            departIdList.push($(this).val());
        });
        var sortList = departIdList.sort();
        for (var i = 0; i < sortList.length - 1; i++) {
            if (sortList[i] == sortList[i + 1]) {
                alertMessage("承办单位重复,请修改!");
                return;
            }
        }

        executeSaveOrUpdate({
            formId: "#formDialog form",
            dialogId: "#formDialog",
            url: getUrl("create"),
            tableId: "#gridTable",
            successMessage: "保存成功",
            successTitle: '保存对话框',
            failMessage: '保存失败',
            failTitle: '消息对话框'
        });
    }

    // 办结评价
    function comment() {
        executeSaveOrUpdate({
            formId: "#formDialog form",
            dialogId: "#formDialog",
            url: getUrl("comment"),
            tableId: "#gridTable",
            successMessage: "保存成功",
            successTitle: '保存对话框',
            failMessage: '保存失败',
            failTitle: '消息对话框'
        });
    }

    //详情
    $("#detail").click(function () {
        showEditDialog({
            tableId: "#gridTable",
            dialogTitle: "任务详情",
            dialogUrl: getUrl("detail"),
            dialogFoot: '<button type="button" class="btn btn-success" onclick="modify()">修改</button>'+
            '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>',
            errorTitle: "消息对话框",
            errorMutiSelectMessage: '您当前选中多行，请重新选择',
            errorUnSelectMessage: '您当前尚未选中一行，请选择后再试',
            errorOk: '确定'
        });
    });

    //修改
    function  modify(){
        executeSaveOrUpdate({
            formId: "#formDialog form",
            dialogId: "#formDialog",
            url: getUrl("update"),
            tableId: "#gridTable",
            successMessage: "保存成功",
            successTitle: '保存对话框',
            failMessage: '保存失败',
            failTitle: '消息对话框'
        });
    }
    //任务流程
    $("#flow").click(function () {
        showEditDialog({
            tableId: "#gridTable",
            dialogTitle: "任务流程",
            dialogUrl: getUrl("flow"),
            dialogFoot: '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>',
            errorTitle: "消息对话框",
            errorMutiSelectMessage: '您当前选中多行，请重新选择',
            errorUnSelectMessage: '您当前尚未选中一行，请选择后再试',
            errorOk: '确定'
        });
    });

    //办结评价
    $("#comment").click(function () {
        var ids = jQuery("#gridTable").jqGrid('getGridParam', 'selarrrow');
        if (ids.length == 1) {
            var rowData = jQuery("#gridTable").jqGrid('getRowData', ids);
           /* if (rowData.type != '已反馈') {
                alertMessage("只有任务状态为已反馈的任务,才可以进行办结评价!");
                return;
            }*/
        }

        showEditDialog({
            tableId: "#gridTable",
            dialogTitle: "办结评价",
            dialogUrl: getUrl("comment"),
            dialogFoot: '<button type="button" class="btn btn-primary" onclick="comment()">提交</button>' +
            '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>',
            errorTitle: "消息对话框",
            errorMutiSelectMessage: '您当前选中多行，请重新选择',
            errorUnSelectMessage: '您当前尚未选中一行，请选择后再试',
            errorOk: '确定'
        });
    });

    //删除
    $("#delete").click(function () {
        showDeleteDialog({
            url: getUrl("delete"),
            tableId: "#gridTable",
            confirmTitle: "确认对话框",
            confirmMessage: "是否确定删除?",
            confirmOk: "确定",
            confirmCancel: "取消",
            successMessage: "删除成功",
            successTitle: "消息对话框",
            failMessage: "删除失败",
            failTitle: "消息对话框",
            errorTitle: "警告提示框",
            errorMessage: "您当前尚未选中一行，请选择后再试",
            errorOk: "确定"
        });
    });

    /**
     * 弹出消息对话框
     * @param msg
     */
    function alertMessage(msg) {
        msg = msg == null ? '' : msg;
        bootbox.dialog({
            title: "消息对话框",
            message: msg,
            buttons: {
                "danger": {
                    "label": '确定',
                    "className": "btn-sm btn-danger"
                }
            }
        });
    }

    /**
     * 打开二级页面
     */
    $("#gridTable").on("dblclick","tr",function () {
        parent.openapp("/project/","team","二级页面")
    })


</script>

</body>
</html>